package spring.tra;

import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.Assert;

public class SimpleService implements FoService {

	public String getFoo(String fooName) {
		return null;
	}

	public String getFoo(String fooName, String barName) {
		return null;
	}

	public void insertFoo(String foo) {
	}

	public void updateFoo(String foo) {
	}

	private final TransactionTemplate transactionTemplate;

	public SimpleService(PlatformTransactionManager manager) {
		Assert.notNull(manager, "can not be null");
		this.transactionTemplate = new TransactionTemplate(manager);
	}

	public void someServiceMethod(final String foo) {
		transactionTemplate.execute(new TransactionCallbackWithoutResult() {

			protected void doInTransactionWithoutResult(TransactionStatus status) {
				try {
					insertFoo(foo);
				} catch (Exception e) {
					status.setRollbackOnly();
				}
			}
		});
	}

}
